﻿#ifndef NET_SOCKET_H
#define NET_SOCKET_H

#include "sys_h/SYS_VER.h"
#include "sys_h/DATATYPE.h"
#include "sys_h/DATATYPE_S.h"
#ifdef CUR_RUN_SYS_WINDOWS_VS2008
#include "WinSock2.h"
#define   A_F PF_INET
static XVOID addr_inet(sockaddr_in adr,XCHAR *&pip)
{
	if (!pip ||
		adr.sin_addr.s_addr==0)
		return;
	XULONG adr_=adr.sin_addr.s_addr;
	pip[0]=adr_%0x100;
	adr_/=0x100;
	pip[1]=adr_%0x100;
	adr_/=0x100;
	pip[2]=adr_%0x100;
	adr_/=0x100;
	pip[3]=adr_%0x100;
	adr_/=0x100;
};
#endif

#ifdef CUR_RUN_SYS_WINDOWS_QT
#include "WinSock2.h"
#define A_F AF_INET
#define socklen_t XINT
static XVOID addr_inet(sockaddr_in adr,XCHAR *&pip)
{
	if (!pip ||
		adr.sin_addr.s_addr==0)
		return;
	XULONG adr_=adr.sin_addr.s_addr;
	pip[0]=adr_%0x100;
	adr_/=0x100;
	pip[1]=adr_%0x100;
	adr_/=0x100;
	pip[2]=adr_%0x100;
	adr_/=0x100;
	pip[3]=adr_%0x100;
	adr_/=0x100;
};
#endif

#ifdef CUR_RUN_SYS_LINUX_QT
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <netdb.h>
#include  <sys/types.h>
#include <sys/socket.h>
#include <QStringList>
#include <QDebug>
#define   A_F AF_INET
#define  SD_BOTH SHUT_RDWR
static XVOID ioctlsocket( XUSHORT socket,XULONG cmd,XULONG *argp)
{
    XINT flag=fcntl(socket,F_GETFL,0);
   fcntl(socket,F_SETFL,O_NONBLOCK);
};
static XVOID closesocket(XUSHORT socket)
{
    close(socket);
};
static in_addr_t inet_addr(XCHAR *pip)
{
    QString   qip=pip;
    in_addr_t ipv4Address = 0;
    if (!pip)
        return ipv4Address;
    if (strlen(pip)<=0)
        return ipv4Address;
    QStringList ipv4 = qip.split(QLatin1String("."));
    if (ipv4.count() != 4)
        return 0;
    uint ip_b=1;
    for (int i = 0; i < 4; ++i)
    {
        bool ok = false;
        uint byteValue = ipv4.at(i).toUInt(&ok);
        if (!ok || byteValue > 255)
            return false;
        //ipv4Address <<= 8;
        byteValue=byteValue*ip_b;
        ipv4Address += byteValue;
        ip_b=ip_b*0x100;
         //qDebug()<<"i"<<i<<"byteValue"<<byteValue<<"\r\n";
    }
    return ipv4Address;
};

static XVOID addr_inet(sockaddr_in adr,XCHAR *&pip)
{
    if (!pip ||
        adr.sin_addr.s_addr==0)
        return;
    XULONG adr_=adr.sin_addr.s_addr;
    pip[0]=adr_%0x100;
    adr_/=0x100;
    pip[1]=adr_%0x100;
    adr_/=0x100;
    pip[2]=adr_%0x100;
    adr_/=0x100;
    pip[3]=adr_%0x100;
    adr_/=0x100;
};
#endif

#endif
